package com.atsmart.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.atsmart.app.BizApp;
import com.atsmart.app.R;
import com.atsmart.app.model.ClientInfo;
import com.atsmart.app.model.ClientListInfo;

import com.atsmart.app.widgets.PlaceHolderImageview;

public class ClientAdapter extends LoadingAdapter {
	private ClientListInfo datasource;
	private LayoutInflater inflater;
//	private boolean fromSQL = false;

	public ClientAdapter(ClientListInfo datasource, Context context) {
		this.datasource = datasource;
//		this.fromSQL = fromSQL;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void appendDataSource(ClientListInfo appendData) {
		datasource.getClients().addAll(appendData.getClients());
	}

	private static class ViewHolder {
		PlaceHolderImageview icon;
		TextView name;
		TextView address;
		TextView tel;
	}

	@Override
	public int getCount() {
		if (datasource.getClients() == null
				|| datasource.getClients().size() == 0) {
			return 0;
		}

		if (delegate != null && delegate.needLoading()) {
			return datasource.getClients().size() + 1;
		} else {
			return datasource.getClients().size();
		}
	}

	@Override
	public Object getItem(int position) {
		if (datasource.getClients() == null
				|| position == datasource.getClients().size()
				&& delegate != null && delegate.needLoading()) {
			return null;
		}
		return datasource.getClients().get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View loadingRow = super.getView(position, convertView, parent);
		if (loadingRow != null) {
			return loadingRow;
		}
		return getView(position, convertView);
	}

	private View getView(final int position, View convertView) {
		ViewHolder vh;
		if (convertView == null || convertView.getTag() == null) {
			convertView = inflater.inflate(R.layout.client_list_item_layout,
					null);
			vh = new ViewHolder();
			vh.icon = (PlaceHolderImageview) convertView
					.findViewById(R.id.img_client_icon);
			vh.name = (TextView) convertView.findViewById(R.id.txt_client_name);
			vh.address = (TextView) convertView
					.findViewById(R.id.txt_client_address);
			vh.tel = (TextView) convertView.findViewById(R.id.txt_client_tel);
//			vh.num = (TextView) convertView
//					.findViewById(R.id.txt_client_tmp_num);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		ClientInfo clientInfo = datasource.getClients().get(position);
		vh.icon.setImageUrl(clientInfo.getImgUrl());
		if (BizApp.getInstance().getCurrentLocation() == null||BizApp.getInstance().getLat()<0.0001||BizApp.getInstance().getLon()<0.0001
				|| clientInfo.getDistance() == 0) {
			vh.name.setText(clientInfo.getName());
		} else if (clientInfo.getDistance() > 0) {
			if (clientInfo.getDistance() < 1000) {
				if(BizApp.getInstance().isWiFiActive() || BizApp.getInstance().isNetworkAvailable()){
					vh.name.setText(clientInfo.getName() + "["
							+ String.format("%.0f", clientInfo.getDistance())
							+ "米]");
//					vh.name.setText(clientInfo.getName());
				}else{
					vh.name.setText(clientInfo.getName());
//					vh.name.setText(clientInfo.getName() + "["
//							+ String.format("%.0f", clientInfo.getDistance())
//							+ "米]");
				}
			} else {
				if (BizApp.getInstance().isWiFiActive() || BizApp.getInstance().isNetworkAvailable()) {
					vh.name.setText(clientInfo.getName()
							+ "["
							+ String.format("%.1f",
									clientInfo.getDistance() / 1000) + "公里]");
//					vh.name.setText(clientInfo.getName());
				} else {
					vh.name.setText(clientInfo.getName());
//					vh.name.setText(clientInfo.getName()
//							+ "["
//							+ String.format("%.1f",
//									clientInfo.getDistance() / 1000) + "公里]");
				}
			}
		}

		vh.address.setText(clientInfo.getAddress());
		vh.tel.setText(clientInfo.getTel());
//		if (clientInfo.getTmpNum() == 0) {
//			vh.num.setVisibility(View.INVISIBLE);
//		} else {
//			vh.num.setVisibility(View.VISIBLE);
//			vh.num.setText(String.valueOf(clientInfo.getTmpNum()));
//		}
		if(position%2==1){
			convertView.setBackgroundResource(R.color.bottom_gray);
		}else{
			convertView.setBackgroundResource(R.color.white);
		}
		return convertView;
	}

	@Override
	protected boolean isLoadingRow(int position) {
		return position >= datasource.getClients().size();
	}

}
